<?php


namespace app\controller;


use app\middleware\Check;
use app\model\FloorModel;
use think\facade\Validate;
use think\Request;
use think\response\Json;
use think\validate\ValidateRule;

class Floor
{
    protected $middleware = [Check::class];

    /**列表
     * @param Request $request
     * @return Json
     */
    public function list(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        $input['name'] = $request->post('name');//
        $input['school_id'] = $request->post('school_id');//学校id
        $input['dormitory_id'] = $request->post('dormitory_id');//学校id
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'dormitory_id' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } else {
                    return true;
                }
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return FloorModel::list($input, $request->post('token'));
    }

    /**添加
     * @param Request $request
     * @return Json
     */
    public function add(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['name'] = $request->param('name');// 名称
        $input['dormitory_id'] = $request->param('dormitory_id');//宿舍楼id
        $input['school_id'] = $request->param('school_id');//学校id
//        $input['floor_conf_id'] = $request->param('floor_conf_id');//配送费id
        // 参数校验
        $validate = Validate::rule([
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'dormitory_id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
//            'floor_conf_id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return FloorModel::add($input, $request->post('token'));
    }

    /**删除
     * @param Request $request
     * @return Json
     */
    public function delete(Request $request){
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['school_id'] = $request->post('school_id');
        $input['dormitory_id'] = $request->post('dormitory_id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'dormitory_id' => ValidateRule::isRequire(null, '缺少参数')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return FloorModel::delete($input, $request->post('token'));
    }


    /**根据ID获取信息
     * @param Request $request
     * @return Json
     */
    public function getOnly(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['school_id'] = $request->post('school_id');
        $input['dormitory_id'] = $request->post('dormitory_id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'dormitory_id' => ValidateRule::isRequire(null, '缺少参数')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return FloorModel::getOnly($input, $request->post('token'));
    }

    /**编辑
     * @param Request $request
     * @return Json
     */
    public function edit(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['name'] = $request->post('name');
//        $input['floor_conf_id'] = $request->post('floor_conf_id');
        $input['school_id'] = $request->post('school_id');
        $input['dormitory_id'] = $request->post('dormitory_id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
//            'floor_conf_id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'dormitory_id' => ValidateRule::isRequire(null, '缺少参数')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return FloorModel::edit($input, $request->post('token'));
    }

    /**获取未绑定的用户
     * @param Request $request
     * @return Json
     */
    public function getUnbindUser(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['page'] = $request->post('page');
        $input['limit'] = $request->post('limit');
        $input['name'] = $request->post('name');
        $input['school_id'] = $request->post('school_id');
        $input['dormitory_id'] = $request->post('dormitory_id');
        // 参数校验
        $validate = Validate::rule([
            'page' => ValidateRule::isRequire(null, '缺少参数'),
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '请输入配送员名称'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'dormitory_id' => ValidateRule::isRequire(null, '缺少参数')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return FloorModel::getUnbindUser($input, $request->post('token'));
    }

    /**绑定用户
     * @param Request $request
     * @return Json
     */
    public function bindUser(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['user_id'] = $request->post('user_id');
        $input['school_id'] = $request->post('school_id');
        $input['dormitory_id'] = $request->post('dormitory_id');
        $input['delivery_floor'] = $request->post('delivery_floor');
        // 参数校验
        $validate = Validate::rule([
            'user_id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'dormitory_id' => ValidateRule::isRequire(null, '缺少参数'),
            'delivery_floor' => ValidateRule::isRequire(null, '缺少参数')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return FloorModel::bindUser($input, $request->post('token'));
    }

    /**解绑用户
     * @param Request $request
     * @return Json
     */
    public function unbindUser(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['school_id'] = $request->post('school_id');
        $input['dormitory_id'] = $request->post('dormitory_id');
        $input['delivery_floor'] = $request->post('delivery_floor');
        $input['user_id'] = $request->post('user_id');
        // 参数校验
        $validate = Validate::rule([
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'dormitory_id' => ValidateRule::isRequire(null, '缺少参数'),
            'user_id' => ValidateRule::isRequire(null, '缺少参数'),
            'delivery_floor' => ValidateRule::isRequire(null, '缺少参数')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return FloorModel::unbindUser($input, $request->post('token'));
    }
}